// 新增/修改页面使用
var seq = 1; // 全局变量
function gval(value) {
	if (typeof (value) == "undefined") {
		return '';
	}
	return value;
}
// 新增行
function addLine(oper, eachLine) { 
	var tbl = document.getElementById("fixedThead");
	var rowSize = tbl.rows.length;
	var row = tbl.insertRow(rowSize);
	var cell0 = row.insertCell(0);
	var cellSeq = row.insertCell(1);
	var cell1 = row.insertCell(2);
	var cell2 = row.insertCell(3);
	var cell3 = row.insertCell(4);
	var cell4 = row.insertCell(5);
	var cell5 = row.insertCell(6);
	var cell6 = row.insertCell(7);
	var cell7 = row.insertCell(8);
	var cell8 = row.insertCell(9);
	var cell9 = row.insertCell(10);
	var cellSel0 = row.insertCell(11);
	var cell10 = row.insertCell(12);
	var cell11 = row.insertCell(13);
	var cell12 = row.insertCell(14);
	cell0.innerHTML = "<input type='checkbox' name='checkLines' style='width:35px'/>";
	cellSeq.innerHTML = seq;
//	cell1.innerHTML = "<input name='contractLineId_" + seq
//			+ "' id='contractLineId_" + seq
//			+ "' style='width:100px' onclick='lovContractLine(" + seq
//			+ ")' readonly value='" + gval(eachLine.contractLineId) + "'/>";
	
//	alert(eachLine.contractLineNum);
	cell1.innerHTML = "<input name='contractLineNum_" + seq
	+ "' id='contractLineNum_" + seq
	+ "' style='width:100px' onclick='lovContractLine(" + seq
	+ ")' readonly value='" + gval(eachLine.contractLineNum) + "'/>";
	
	// cell2 = 物料id cell3 = 物料编码
	cell2.innerHTML = "<input id='materielId_" + seq + "' name='materielId_"
			+ seq + "' type='hidden' value='" + gval(eachLine.materielId)
			+ "'/><input name='segment1_" + seq + "' id='segment1_" + seq
			+ "' style='width:100px' onclick='lovMateriel(" + seq
			+ ")' readonly value='" + gval(eachLine.segment1) + "'/>";
	// 实际展示的是cell3的值 ,有影藏表单域的需要放在一个单元格，否则样式会错乱
	// input + 1
	cell3.innerHTML = "<input name='materielName_" + seq
			+ "' id='materielName_" + seq
			+ "' style='width:100px' readonly value='"
			+ gval(eachLine.materielName) + "' onclick='lovMateriel(" + seq
			+ ")'/>";
	cell4.innerHTML = "<input name='materielVersionNum_" + seq
			+ "' id='materielVersionNum_" + seq
			+ "' style='width:80px' readonly value='"
			+ gval(eachLine.materielVersionNum) + "'/>";
	cell5.innerHTML = "<input name='materielVersionName_" + seq
	+ "' id='materielVersionName_" + seq
	+ "' style='width:80px' readonly value='"
			+ gval(eachLine.materielVersionName) + "'/>";
//	cell7.innerHTML = "<input name='materielVersionModel_" + seq
//	+ "' id='materielVersionModel_" + seq
//	+ "' style='width:80px' readonly value='"
//			+ gval(eachLine.materielVersionModel) + "'/>";
//	cell8.innerHTML = "<input name='materielSpace_" + seq
//	+ "' id='materielSpace_" + seq
//	+ "' style='width:80px' readonly value='"
//			+ gval(eachLine.materielSpace) + "'/>";
	cell6.innerHTML = "<input name='materielQuantity_"
			+ seq
			+ "' id='materielQuantity_"
			+ seq
			+ "' style='ime-mode:disabled;width:100px' onkeypress='return myNumberic(event)'onpaste='return false' ondragenter='return false' oncontextmenu='return false;' value='"
			+ gval(eachLine.materielQuantity) + "'/>";
	// 添加采购单价 -- //
	cell7.innerHTML = "<input name='materielUnitPrice_" + seq
	+ "' id='materielUnitPrice_" + seq
	+ "'  style='ime-mode:disabled;width:80px' onkeypress='return myNumberic(event)'onpaste='return false' ondragenter='return false' oncontextmenu='return false;' value='"
			+ gval(eachLine.materielUnitPrice) + "'/>";
	cell8.innerHTML = "<input name='totalAmount_"
			+ seq
			+ "' id='totalAmount_"
			+ seq
			+ "'onblur='calcSum()' style='ime-mode:disabled;width:100px' onkeypress='return myNumberic(event)' onpaste='return false' ondragenter='return false' oncontextmenu='return false;' value='"
			+ gval(eachLine.totalAmount) + "'/>";
//	cell11.innerHTML = "<select id='taxRate_"
//			+ seq
//			+ "' name='taxRate_"
//			+ seq
//			+ "'><option value='0'>请选择</option><option value='0.03'>3%</option><option value='0.06'>6%</option><option value='0.11'>11%</option><option value='0.13'>13%</option><option value='0.17'>17%</option></select>";
//	cell12.innerHTML = "<input name='taxmoneyNumber_"
//			+ seq
//			+ "' id='taxmoneyNumber_"
//			+ seq
//			+ "' style='ime-mode:disabled;width:100px' onkeypress='return myNumberic(event)'onpaste='return false' ondragenter='return false' oncontextmenu='return false;' value='"
//			+ gval(eachLine.taxmoneyNumber) + "'/>";
//	cell13.innerHTML = "<input name='intaxmoneyNumber_"
//			+ seq
//			+ "' id='intaxmoneyNumber_"
//			+ seq
//			+ "' style='ime-mode:disabled;width:100px' onkeypress='return myNumberic(event)'onpaste='return false' ondragenter='return false' oncontextmenu='return false;' value='"
//			+ gval(eachLine.intaxmoneyNumber) + "'/>";
	cell9.innerHTML = "<input class='easyui-datebox' type='text' editable='false' style='width: 200px; height: 22px;' name='deliveryPlanningDate_"
			+ seq + "' id='deliveryPlanningDate_" + seq + "'/>";
	cellSel0.innerHTML = "<select id='status_"
		+ seq
		+ "' name='status_"
		+ seq
		+ "'><option value='ORDERED'>已下单</option><option value='SHIPPED'>已发货</option></select>";
	// cell10.innerHTML = "<input class='easyui-datebox' style='width: 200px;
	// height: 22px;' name='purchaseOrderDate3' editable='false'
	// id='purchaseOrderDate3' type='text' />";  
	cell10.innerHTML = "<input name='remark_" + seq + "' id='remark_" + seq  
			+ "' style='width:100px' value='" + gval(eachLine.remark) + "'/>";
	cell11.innerHTML = "<input name='poLineId_" + seq + "' id='poLineId_" + seq
			+ "' style='width:100px' type='hidden' value='"
			+ gval(eachLine.poLineId) + "'/>"; 
	cell12.innerHTML = "<input name='contractLineId_" + seq
	+ "' id='contractLineId_" + seq
	+ "' type='hidden' value='" + gval(eachLine.contractLineId) + "'/>";

//	var taxRateSel = "#taxRate_" + seq + " option";
//	$(taxRateSel).each(function() {
//		if ($(this).val() == gval(eachLine.taxRate)) {
//			$(this).attr("selected", true)
//			// $(this).attr("selected","selected")两者都可以选中
//		}
//	});
	var status = "#status_" + seq + " option";
	$(status).each(function() {
		if ($(this).val() == gval(eachLine.status)) {
			$(this).attr("selected", true)
			// $(this).attr("selected","selected")两者都可以选中
		}
	});
	var deliveryPlanningDateId = "#deliveryPlanningDate_" + seq;
//	alert('NEWLINE:' + deliveryPlanningDateId);
	$.parser.parse($(deliveryPlanningDateId).parent());
	// 
	// var tmpDateValue = gval(eachLine.deliveryPlanningDate);
	// if(tmpDateValue=='') {
	// $(deliveryPlanningDateId).datebox('setValue',
	// formatDate(new Date()));
	// }else {
	$(deliveryPlanningDateId).datebox('setValue',
			formatDate(new Date(gval(eachLine.deliveryPlanningDate))));
	// }
	seq++;
}
function calcSum() {
	var valueTable = document.getElementById("fixedThead");
	var table_tr = valueTable.getElementsByTagName('tr');
	// lookupCodeArr = [];
	var sumAmount = 0.00;
	for (var i = 1; i < table_tr.length; i++) {
		var inputParam = table_tr[i].getElementsByTagName('input');
		// 行的金额
		sumAmount = Number(sumAmount) + Number(inputParam[9].value);
	}
	$("#sumAmount").val(sumAmount);
}

// 删除行
function deleteLine() {
	var deleteIds = "";
	var ckbs = $("input[name='checkLines']:checked");
	if (ckbs.length == 0) {
		$.messager.alert('删除指定行', '要删除指定行，需选中要删除的行！', 'info');
		return;
	} else {
		ckbs.each(function() {
//			alert('\\ ' + $(this).val() );
//			if ($(this).val() != "") {
//				deleteIds = deleteIds + $(this).val() + ",";
//			}
			$(this).parent().parent().remove();
		});
//		$("#deleteIds").val(deleteIds);
	}
}
// common begin ===============================================================
// 新增
function addEntity() {
	if (checkmain()) {
		$.ajax({
			url : webUrl + 'order/save.do',
			type : 'post',
			data : {
				param : buildEntity('add')
			}
		}).done(
				function(data) {
					var result = data.bo;
					if (result == 0) {
						$.messager.confirm('提示 ', '保存成功！', function(r) {
							$('.main_panel').load(
									webUrl + "public/page/order/query.html");
						});

					} else if (result == -1) {
						$.messager.alert('提示 ', '【订单编号】已经存在！', 'info');
					} else {
						$.messager.alert('提示 ', '保存失败！', 'info');
					}
				});
	}
}

function checkmain() {
	if (!checknull("segment1")) {
		return false;
	}
	var valueTable = document.getElementById("fixedThead");
	var table_tr = valueTable.getElementsByTagName('tr');
	// lookupCodeArr = [];
	for (var i = 1; i < table_tr.length; i++) {
		var inputParam = table_tr[i].getElementsByTagName('input');
		// lookupCodeArr[i - 1] = inputParam[1].value;
		// 校验索引 物料编码* 数量* 金额*
		// 去buildEntity方法找索引
		if (!checknull(inputParam[2].name, inputParam[7].name,
				inputParam[9].name)) {
			return false;
		}
	}
	// 判断订单编号不能重复
	return true;
}

// 修改
function updateEntity() {
	if (checkmain()) {
		$.ajax({
			url : webUrl + 'order/update.do',
			type : 'post',
			data : {
				param : buildEntity('update')
			}
		}).done(
				function(data) {
					var result = data.bo;
					if (result == 0) {
						$.messager.confirm('提示 ', '修改成功！', function(r) {
							$('.main_panel').load(
									webUrl + "public/page/order/query.html");
						});
					} else {
						$.messager.alert('提示 ', '修改失败！', 'info');
					}
				});
	}
}
// 构建修改
function buildEntity(oper) {
	var json = {};
	json.chwPoHeader = {};
	if (oper == 'update') {
		json.chwPoHeader.poHeaderId = $("#poHeaderId").val();// 修改用到
		// json.chwPoHeader.lastUpdatedBy = $("#lastUpdatedBy").val();// 修改用到
		// json.chwPoHeader.createdBy = $("#createdBy").val();// 修改用到
		// // alert( $("#createdDate").datetimebox('getValue'));
		// json.chwPoHeader.createdDate = $("#createdDate")
		// .datetimebox('getValue');// 修改用到 时分秒
		// json.chwPoHeader.lastUpdatedDate = $("#lastUpdatedDate").datetimebox(
		// 'getValue');// 修改用到 时分秒
		// json.chwPoHeader.enabledFlag = $("#enabledFlag").val();// 修改用到
		// json.chwPoHeader.status = $("#status").val();// 修改用到
	}
	json.chwPoHeader.segment1 = $("#segment1").val();
	json.chwPoHeader.vendorId = $("#vendorId").val();
	// json.chwPoHeader.vendorName = $("#vendorName").val();
	json.chwPoHeader.buyer = $("#buyer").val();
	// json.chwPoHeader.buyerName = $("#buyerName").val();
	json.chwPoHeader.remark = $("#remark").val();
	// json.chwPoHeader.purchaseOrderMaker = $("#purchaseOrderMaker").val();
	if ($("#purchaseOrderDate").datebox('getValue') != "") {
		json.chwPoHeader.purchaseOrderDate = $("#purchaseOrderDate").datebox(
				'getValue');
	}
	// 
	json.chwPoHeader.status = $('#SelStatus option:selected').val();
	json.chwPoHeader.priority = $('#SelPriority option:selected').val();
	json.lineList = [];
	var valueTable = document.getElementById("fixedThead");
	var table_tr = valueTable.getElementsByTagName('tr');
	for (var i = 0; i < table_tr.length -1; i++) {
		var inputParam = table_tr[i+1].getElementsByTagName('input');
		var selectParam = table_tr[i+1].getElementsByTagName('select');
//		alert(inputParam[1].value);
//		alert(inputParam[15].value);
		var Values = {};
		Values = {
			// 	checkbox// inputParam[0]
			//"contractLineId" : inputParam[1].value, // contractLineNum
"contractLineNum":inputParam[1].value,
			"contractLineId" : inputParam[15].value,
			"materielId" : inputParam[2].value,
			"segment1" : inputParam[3].value,
			// materielName : inputParam[4].value,
			"materielVersionNum" : inputParam[5].value,
			"materielVersionName" : inputParam[6].value,
//			"materielVersionModel" : inputParam[7].value,
			"materielQuantity" : inputParam[7].value,
			"materielUnitPrice" : inputParam[8].value,
			"totalAmount" : inputParam[9].value,
			//"taxRate" : selectParam[0].value,
			//"taxmoneyNumber" : inputParam[11].value,
			//"intaxmoneyNumber" : inputParam[12].value,
			
			"status" : selectParam[0].value,
			// deliveryPlanningDate: inputParam[10 - 11].value
			// 日期空间占用3个input?
			"remark" : inputParam[13].value
		};
//		alert('属性名：' + inputParam[12].name);
		var nameid = "#" + inputParam[12].name;
//		alert($(nameid).datebox('getValue'));
//		var dateId = "#deliveryPlanningDate_" + i;
		// 此处如果序号被删除  日期的取值序号会变化，不能用dateId来表示; 比如索引会取不到值
		if ($(nameid).datebox('getValue') != "") {
			Values.deliveryPlanningDate = $(nameid).datebox('getValue');
		}
		if (oper == 'update') {
			Values.poLineId = inputParam[14].value; // 修改用到
		}
		json.lineList.push(Values);
	}
	json = JSON.stringify(json);
	return json;
}
// common end =================================================================
$("ul.tabtit").on("click", function(event) {
	$('.main_panel').load(webUrl + "public/page/order/query.html");
});

function cancel() {
	$('.main_panel').load(webUrl + "public/page/order/query.html");
}